package com.adong.rabbitmq.routing;

import com.adong.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RoutingProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "test_direct";
        // 创建交换机
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, false, null);
        // 创建队列
        String queue1Name = "test_direct_queue1";
        String queue2Name = "test_direct_queue2";
        // 声明（创建）队列
        channel.queueDeclare(queue1Name, true, false, false, null);
        channel.queueDeclare(queue2Name, true, false, false, null);

        // 队列绑定交换机
        // 队列1绑定error
        /*
            1. queue：队列名称
            2. exchange：交换机名称
            3. routingKey：路由键,绑定规则
        */
        channel.queueBind(queue1Name, exchangeName, "error");

        // 队列2绑定info error warning
        channel.queueBind(queue2Name, exchangeName, "info");
        channel.queueBind(queue2Name, exchangeName, "error");
        channel.queueBind(queue2Name, exchangeName, "warning");

        String message = "日志信息：张三调用了delete方法.错误了,日志级别warning";

        // 发送消息
        /*
            参数1：交换机名称，如果没有指定则使用默认Default Exchange
            参数2：路由key
            参数3：消息其它属性
            参数4：消息内容
        */
        channel.basicPublish(exchangeName, "error", null, message.getBytes());
        System.out.println(message);

        channel.close();
        connection.close();
    }


}
